package com.example.star.view.fragment;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.star.R;
import com.example.star.adapter.PostAdapter;
import com.example.star.api_service.PostApi;
import com.example.star.entity.Post;
import com.example.star.retrofit.ApiClient;
import com.example.star.view.activity.AddPostActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PostFragment extends Fragment {
    private PostApi postApi;
    private ListView postListView;
    private Button addPostButton;
    private List<Post> postList = new ArrayList<>();
    private PostAdapter postAdapter;
    private String userName; // 假设这是当前登录用户的用户名

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_post, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化视图组件
        postListView = view.findViewById(R.id.post_list_view);
        addPostButton = view.findViewById(R.id.add_post_button);

        // 从 SharedPreferences 中读取用户名
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("user_info", MODE_PRIVATE);
        userName = sharedPreferences.getString("username", "");

        // 通过 ApiClient 获取 PostApi 实例
        postApi = ApiClient.getPostApi();

        // 初始化适配器，使用自定义布局
        postAdapter = new PostAdapter(requireContext(), R.layout.item_post, postList, userName, this);
        postListView.setAdapter(postAdapter);

        // 加载帖子列表
        getPosts();
        setupClickListeners();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 查询列表时刷新
        getPosts();
    }

    private void setupClickListeners() {
        // 设置添加帖子按钮的点击事件
        addPostButton.setOnClickListener(v -> {
            // 启动 AddPostActivity
            Intent intent = new Intent(requireContext(), AddPostActivity.class);
            startActivity(intent);
        });
    }

    private void getPosts() {
        Call<List<Post>> call = postApi.getAllPosts();
        call.enqueue(new Callback<List<Post>>() {
            @Override
            public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                if (response.isSuccessful()) {
                    postList.clear();
                    postList.addAll(response.body());
                    postAdapter.notifyDataSetChanged();
                    for (Post post : postList) {
                        Log.d("PostFragment", "Title: " + post.getTitle());
                    }
                } else {
                    Log.e("PostFragment", "Failed to get posts, code: " + response.code());
                }
            }

            @Override
            public void onFailure(Call<List<Post>> call, Throwable t) {
                Log.e("PostFragment", "Error: " + t.getMessage());
            }
        });
    }
}